package com.wl.dzt.sys.core.dao.curd.validate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 验证器
 * 解析和执行验证
 */
@Component
public class Validatetor {

    /**
     * <b>执行验证</b>
     * <p>
     * 根据参数执行验证 <br>
     * 成功则返回空对象<br>
     * 失败返回当前失败的验证规则
     * </p>
     *
     * @param entity
     * @param validates
     * @return
     */
    public List<Validate> excuteValidate(Object entity, Validate... validates) {
        List<Validate> list = new ArrayList<>();
        for (Validate validate : validates) {
            boolean success = true;
            String frist = validate.getExpsFirst();
            switch (frist) {
                // 删除验证
                case "!": {
                    success = validate.deleteRefValidate(entity);
                    break;
                }
                // spring 验证
                case "#": {
                    success = validate.springValidate(entity);
                    break;
                }
                // 字段存在验证
                default: {
                    success = validate.fieldExitsValidate(entity);
                    break;
                }
            }
            if (!success) {
                list.add(validate);
            }
        }
        return list.isEmpty() ? null : list;
    }
}
